<?php

namespace frontend\models;

/**
 * @author  shuang
 * @date    2016-11-4 22:23:03
 * @version V1.0
 * @desc    
 */
use common\models\UserTaskOperate;
use Yii;
use yii\db\Query;
class WB_UserTaskOperate extends UserTaskOperate {

    public static $complain = [
        0 => "投诉",
        1 => "投诉中",
        2 => "投诉成功",
        3 => "投诉失败",
        4=>"申诉中",
        5=>"申诉成功",
        6=>"申诉失败"
    ];

    /*
     * 查看任务
     * @params $taskid
     * return array
     */

    public static function getOperateTask($taskid) {
        $query = WB_UserTaskOperate::find()->where("taskid=$taskid and status=1")->orderBy("created_at desc");
        $countQuery = clone $query;
        $pagesize = 100;
        $offset = (Yii::$app->request->get("page") - 1) * $pagesize;
        $limit = $pagesize;
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => $pagesize,"pageSizeLimit"=>[1,100]]);
        $res = $query->offset($offset)->limit($limit)->asArray()->all();
        return ["pager" => $pager, "data" => $res, "count" => WB_UserTaskOperate::find()->where("taskid=$taskid")->count()];
    }

    /*
     * 投诉任务
     * @params $id
     * @params $cont
     * return boolean
     */

    public static function complainTask($id, $cont) {
        $model = WB_UserTaskOperate::findOne($id);
        if ($model instanceof WB_UserTaskOperate) {
            $model->is_complain = 1;
            $model->complain_at = time();
            $model->complain_note = $cont;
            return $model->update();
        } else {
            throw new \yii\web\NotFoundHttpException;
        }
    }

    /*
     * 查询投诉任务
     */
    public static function getComplainTaskList() {
        $query = new Query();
        $query->from(WB_UserTaskOperate::tableName() . " as a");
        $query->leftJoin(WB_Task::tableName() . " as b", "a.taskid=b.id");
        $query->leftJoin(WB_TaskPrice::tableName() . " as c", "b.task_price_id=c.id");
        $query->select("a.id,a.updated_at,a.task_image,a.taskid,a.status,a.is_hidden,a.is_complain,b.title,a.complain_note,c.brush_price");
        $query->where("a.userid=:userid and is_complain = 1 or is_complain=2", [":userid" => Yii::$app->user->id]);
        $countQuery = clone $query;
        $defaultPageSize = 20;
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => $defaultPageSize]);
        $offset = (Yii::$app->request->get("page") - 1) * $defaultPageSize;
        $limit = $defaultPageSize;
        $res = $query->offset($offset)->limit($limit)->all();
        return ["pager" => $pager, "data" => $res];
    }
}
